package com.wdb007.venus.web.api.controller;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.wdb007.venus.svc.facade.BookShelfService;
import com.wdb007.venus.svc.facade.book.model.BookShelfDTO;
import com.wdb007.venus.svc.facade.constant.RspCodeEnum;
import com.wdb007.venus.svc.facade.exception.BizException;
import com.wdb007.venus.web.api.book.model.BookShelfRequest;
import com.wdb007.venus.web.api.book.model.BookShelfVO;
import com.wdb007.venus.web.api.model.BaseResponse;

@Controller
@RequestMapping("/bookshelf")
public class BookshelfController extends BaseController{
	
	private final static Logger logger = LoggerFactory.getLogger(BookshelfController.class);
	@Autowired
	private BookShelfService bookShelfService;
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	@ResponseBody
	public BaseResponse<BookShelfVO> list(){
		BaseResponse<BookShelfVO> response = success();
		try {
			response.setResult(1);
			response.setCode(0);
			List<BookShelfDTO> list = bookShelfService.list();
			if(list == null){
				response.setResult(0);
				response.setCode(400);
			}else {
				for(BookShelfDTO dto : list){
					BookShelfVO model = new BookShelfVO();
					BeanUtils.copyProperties(dto, model);
					response.getItems().add(model);
				}
				response.setTotalcount(list.size());
			}
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			response = failed(e);
		}
		return response;
	}
	
	/**
	 * 根据前端传递的用户坐标来查询周围三公里以内的书柜坐标
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/getCoordinates", method = RequestMethod.GET)
	@ResponseBody
	public BaseResponse getCoordinates(BookShelfRequest request){
		logger.info("获取当前坐标三公里以内的书柜坐标请求参数： {}", JSON.toJSONString(request));
		
		BaseResponse response = success();
		try {
			checkIsNull(request.getMap_type(), "map_type");
			checkIsNull(request.getUser_latitude(), "user_latitude");
			checkIsNull(request.getUser_longitude(), "user_longitude");
			BookShelfDTO reqParam = new BookShelfDTO();
			reqParam.setLatitude(request.getUser_latitude());
			reqParam.setLongitude(request.getUser_longitude());
			Integer mapType  = request.getMap_type();
			List<BookShelfDTO> list = bookShelfService.listByLocation(reqParam, mapType);
			if(list == null)
				throw new BizException(RspCodeEnum.QUERY_LIST_IS_NULL);
			
			for(BookShelfDTO dto : list){
				BookShelfVO model = new BookShelfVO();
				BeanUtils.copyProperties(dto, model);
				response.getItems().add(model);
			}
			response.setTotalcount(list.size());
			
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
			response = failed(e);
		}
		logger.info("获取当前坐标三公里以内的书柜坐标响应 : {}", JSON.toJSONString(response));
		return response;
	}
}
